package com.gl.multipleyes.api

import com.gl.multipleyes.User;

import grails.plugin.facebooksdk.FacebookGraphClient

class MultipleyesController {
	
	def checkedOperation (def closure) {
		def accessToken = request.getHeader("accessToken")
		def facebookClient = new FacebookGraphClient(accessToken)
		def fbuser = facebookClient.fetchObject("me") // Requires a user access token

		closure.params.user = User.findByExternalId(fbuser.id)?User.findByExternalId(fbuser.id):new User(externalId: fbuser.id,externalToken:accessToken,name:fbuser.name).save(flush:true, failOnError:true)
		return closure()
	}

}
